package adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import net.ck.hzq.R;

import java.util.ArrayList;
import java.util.List;

import Bean.Problems;

public class LearnMibbleAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final Context mContext;
    private List<Problems> learnQueslist;
    private String mcontent="";//关键字

    public LearnMibbleAdapter(Context context) {
        mContext = context;
        learnQueslist = new ArrayList<>();
    }

    public void setupData(List<Problems> learnQueslists) {
        if (learnQueslist != null) {
            learnQueslist = learnQueslists;
        }
        notifyDataSetChanged();
    }
    public void setupData(List<Problems> learnQueslists,String content) {
        if (learnQueslist != null) {
            learnQueslist = learnQueslists;
        }
        mcontent = content;
        notifyDataSetChanged();
    }
    @Override
    public int getItemCount() {
        return learnQueslist.size();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_learnrecycleview, viewGroup, false);
//        view.setOnLongClickListener(this);
        return new DividerVH(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holders, int i) {
        DividerVH holder = (DividerVH) holders;

        if(learnQueslist.get(i).getTitle().contains(mcontent)){
            holder.learn_titlecontent.setText(setColor(mContext,learnQueslist.get(i).getTitle(),mcontent));
        }else{
            holder.learn_titlecontent.setText(learnQueslist.get(i).getTitle());
        }

        if(learnQueslist.get(i).getContent().contains(mcontent)){
            holder.lear_textcontent.setText(setColor(mContext,learnQueslist.get(i).getContent(),mcontent));
        }else{
            holder.lear_textcontent.setText(learnQueslist.get(i).getContent());
        }

        holder.learn_title.setOnClickListener(v -> {
            if (holder.learn_content.getVisibility() == View.GONE) {
                holder.learn_content.setVisibility(View.VISIBLE);
                holder.leran_titleimg.setImageResource(R.mipmap.learn_top);
            } else {
                holder.learn_content.setVisibility(View.GONE);
                holder.leran_titleimg.setImageResource(R.mipmap.learn_bottom);
            }
        });
    }
    public static CharSequence setColor(Context context, String text, String text1) {
        int tag = text.indexOf(text1);
        SpannableStringBuilder style = new SpannableStringBuilder(text);
// 关键字“孤舟”变色，0-text1.length()
        style.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.red_fe3333)), tag,tag + text1.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return style;
    }
    public class DividerVH extends RecyclerView.ViewHolder {
        private RelativeLayout learn_title;
        private RelativeLayout learn_content;
        private ImageView leran_titleimg;
        private TextView learn_titlecontent;
        private TextView lear_textcontent;

        DividerVH(@NonNull View itemView) {
            super(itemView);
            learn_titlecontent = itemView.findViewById(R.id.learn_titlecontent);
            lear_textcontent = itemView.findViewById(R.id.lear_textcontent);
            learn_title = itemView.findViewById(R.id.learn_title);
            leran_titleimg = itemView.findViewById(R.id.leran_titleimg);
            learn_content = itemView.findViewById(R.id.learn_content);
        }
    }


}

